/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.util.ArrayList;
import java.util.List;
import modelo.Professor;
import org.hibernate.classic.Session;
import persistence.HibernateUtil;

/**
 *
 * @author Sh4c4w
 */
public class ProfessorDao {

    Session sessao = null;

    public void add(Professor professor) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.save(professor);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public Professor getProfById(Long id) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        Professor p = (Professor) sessao.get(Professor.class, id);
        sessao.getTransaction().commit();
        sessao.close();
        return p;
    }
    
    public void update (Professor professor){
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.update(professor);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public List<Professor> list() {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        List<Professor> lista = new ArrayList<Professor>();
        lista = sessao.createQuery("from Pessoa where DTYPE = 'Professor'").list();
        sessao.getTransaction().commit();
        sessao.close();
        return lista;
    }

    public boolean remove(Professor professor) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.delete(professor);
        sessao.getTransaction().commit();
        sessao.close();
        return true;
    }
}
